# --*-- coding=utf-8 --*--
import csv


def read_csv(fp, delimiter=",", fieldnames=None):
    """
    :param fp:
    :param delimiter:
    :param fieldnames:
    :return:
    """
    records = []
    with open(fp, 'r', encoding='utf-8') as f:
        reader = csv.reader(f, delimiter=delimiter)
        if fieldnames is None:
            fieldnames = next(reader)
        csv_reader = csv.DictReader(f, fieldnames=fieldnames, delimiter=delimiter)
        for row in csv_reader:
            records.append(row)
    return records


def write_csv(fp, records):
    """
    :param fp:
    :param records:
    """
    with open(fp, 'a', newline='', encoding='utf-8') as f:
        writer = csv.writer(f, dialect="excel")
        if isinstance(records, list):
            for record in records:
                if isinstance(record, list):
                    writer.writerow(record)
                elif isinstance(record, dict):
                    writer.writerows(record.values())
        elif isinstance(records, dict):
            writer.writerows(records)


if __name__ == '__main__':
    records = read_csv('./test.csv')
    print(records)
